package com.mico.echart.monitor.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author micocube
 * projectName: springboot-example
 * packageName: com.mico.echart.monitor.config
 * email: ldscube@gmail.com
 * createTime: 2019-12-30 13:41
 * version: 0.1
 * description:
 */
public class GlobalObject {
    private static Logger logger = LoggerFactory.getLogger(GlobalObject.class);
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

    public static String writeValueAsString(Object object,boolean pretty){
        try {
            String valueAsString = pretty? OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object):OBJECT_MAPPER.writeValueAsString(object);
            return valueAsString;
        } catch (JsonProcessingException e) {
            logger.error("json write error:",e);
            return "";
        }
    }

    public static String writeValueAsString(Object object){
        return writeValueAsString(object,false);
    }
}
